//readWriteToFlash.c

/* Standard includes */
#include <stdint.h>
//#include <msp430xG46x.h>

/* Scheduler includes */
#include "FreeRTOS.h"
#include "task.h"

/* Application includes */
#include "readWriteToFlash.h"


/* function to write to flash memory */
//Input: value to be written to flash
//Output: none
void writeToInfoMem
(
  uint8_t valueToWrite
)
{
    uint16_t* FlashPtrForWrite;     //initialize flash pointer
    
    FlashPtrForWrite = (uint16_t*)0x01800;  //select address in info. memory
    
    __disable_interrupt();  //disable/re-enable interrupts
    
    FCTL3 = FWKEY;          //Clear Lock bit (unlock flash)
    FCTL1 = FWKEY + ERASE;  //Set Erase bit
    
    *FlashPtrForWrite = 0;  //Dummy write to erase Flash word
    
    FCTL1 = FWKEY + WRT;    //Set WRT bit for write operation
                            //(enable flash write)
    
    *FlashPtrForWrite = valueToWrite; //write last LCD value to flash
    
    FCTL1 = FWKEY;          //Clear WRT bit (disable flash write)
    FCTL3 = FWKEY + LOCK;   //Set LOCK bit (lock flash)
    
    _EINT();                //enable interrupts   
    
}

/* function to read from flash memory */
//Input: point to location in flash
//Output: value read from flash
uint8_t ReadFromInfoMem
(
  uint16_t* flashPtr
)
{
  uint8_t valueToRead = 0;
  
  __disable_interrupt();  //disable interrupts
     
  valueToRead = *flashPtr;
          
  _EINT();                //enable interrupts   
  
  return valueToRead;
}

/************* Added FreeRTOS functionality **************/

//void vStartReadFlashTasks( unsigned portBASE_TYPE uxPriority )
//{
//     
//  /* create the task */
//  /*declare and initialize variables */
//  uint8_t valueFromFlash = 0; 
//  uint16_t* flashPtrForRead;
//  flashPtrForRead = (uint16_t*)0x01800;
//
//  valueFromFlash = ReadFromInfoMem(flashPtrForRead);  //read from flash
//     
//  /* Spawn the task */
////  xTaskCreate(  );
//   
//}

void vStartWriteFlashTasks( unsigned portBASE_TYPE uxPriority )
{
  /* create the task */ 
  uint8_t valueToLCD = 0; //value to display on LCD
  writeToInfoMem(valueToLCD);

  /* Spawn the task */
//  xTaskCreate(  );
//     
}

